<?php

/**
 * This is the model class for table "alumni".
 *
 * The followings are the available columns in table 'alumni':
 * @property integer $id
 * @property string $alumni_registration_date
 * @property string $alumni_company_name
 * @property string $alumni_company_contact_num
 * @property string $company_address
 * @property string $alumni_occupation
 * @property integer $user_id
 *
 * The followings are the available model relations:
 * @property User $user
 */
class Alumni extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Alumni the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'alumni';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('alumni_registration_date, user_id', 'required'),
			array('user_id', 'numerical', 'integerOnly'=>true),
			array('alumni_company_name, company_address', 'length', 'max'=>45),
			array('alumni_company_contact_num', 'length', 'max'=>7),
			array('alumni_occupation', 'length', 'max'=>100),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, alumni_registration_date, alumni_company_name, alumni_company_contact_num, company_address, alumni_occupation, user_id', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'user' => array(self::BELONGS_TO, 'User', 'user_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'alumni_registration_date' => 'Registration Date',
			'alumni_company_name' => 'Company Name',
			'alumni_company_contact_num' => 'Contact number',
			'company_address' => 'Company Address',
			'alumni_occupation' => 'Occupation',
			'user_id' => 'User',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('alumni_registration_date',$this->alumni_registration_date,true);
		$criteria->compare('alumni_company_name',$this->alumni_company_name,true);
		$criteria->compare('alumni_company_contact_num',$this->alumni_company_contact_num,true);
		$criteria->compare('company_address',$this->company_address,true);
		$criteria->compare('alumni_occupation',$this->alumni_occupation,true);
		$criteria->compare('user_id',$this->user_id);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	public function behaviors()
	{
  	  return array(
        'LoggableBehavior'=>
            'application.modules.auditTrail.behaviors.LoggableBehavior',
   	 );
	}
	
	public function getAlumniName()
	{
		return $this->user->alumniName;
	}

	public function getUserCode()
	{
		return $this->user->userCode;
	}

	public function AlumniCount()
	{
		$criteria = new CDbCriteria;
		$criteria->select='COUNT(*) as totalAlumni';
		$criteria->condition='roles = :searchTxt';
		$criteria->params=array(':searchTxt'=>'alumni');
		$user = User::model()->find($criteria);
		return $user->totalAlumni;
	}

	public function Renew()
	{
		if(count($this->user->volunteers) == 0){

				$volunteer = new Volunteer;
				$volunteer->volunteer_start_date= date("Y-m-d");
				$volunteer->user_id = $this->user_id;
				$volunteer->save();
		}

		$this->user->roles = 'volunteer';
		$this->user->save();
	}	
}